//
//  PoiSearchTableView.m
//  wtshoppingcite
//
//  Created by TW on 15-3-19.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "PoiSearchTableView.h"
#import <BaiduMapAPI/BMapKit.h>

@implementation PoiSearchTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    
    self = [super initWithFrame:frame style:style];
    if(self){
        self.backgroundView = nil;
        self.backgroundColor = [UIColor whiteColor];
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    return self;
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 80;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"poiSearchCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"poiSearchCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *titleLabel = [self createLabelWithFrame:CGRectMake(5, 10, kWidth-60, 20) text:@"" font:16 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
        titleLabel.tag = 100;
        [cell.contentView addSubview:titleLabel];
        
        
         UILabel *subtitleLabel = [self createLabelWithFrame:CGRectMake( 5, titleLabel.bottom+5, kWidth-60, 40) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_GRAY_COLOR];
        subtitleLabel.tag = 101;
        subtitleLabel.numberOfLines = 2;
        [cell.contentView addSubview:subtitleLabel];
        
    
        UIButton *telButton = [[UIButton alloc]initWithFrame:CGRectMake(titleLabel.right+10, titleLabel.top , 40, 40)];
        telButton.tag = 102;
        [telButton setTitleColor:UIColorFromRGB(0x9fa0a0) forState:UIControlStateNormal];
        [telButton setFont:[UIFont systemFontOfSize:14.f]];
        telButton.right = kWidth - 15;
        [cell.contentView addSubview:telButton];
        
        
        UILabel *distanceLabel = [self createLabelWithFrame:CGRectMake(subtitleLabel.right, telButton.bottom, 60, 40) text:@"" font:12 textAli:NSTextAlignmentLeft textColor:MAIN_GRAY_COLOR];
        distanceLabel.tag = 103;
        [cell.contentView addSubview:distanceLabel];
        
    }
    
    BMKPoiInfo *poiInfo = self.data[indexPath.row];
    
    UILabel *titleLabel =(UILabel *)[cell.contentView viewWithTag:100];
    titleLabel.text = poiInfo.name;
    
    UILabel *subtitleLabe =(UILabel *)[cell.contentView viewWithTag:101];
    subtitleLabe.text = poiInfo.address;
    
    
    UIButton *telButton = (UIButton *)[cell.contentView viewWithTag:102];
    
    if (![poiInfo.phone isEqualToString:@"<null>"] || ![poiInfo.phone  isEqual: @""] || [poiInfo.phone  isEqualToString:@"(null)"]) {
        
        [telButton setImage:[UIImage imageNamed:@"register_tel"] forState:UIControlStateNormal];
        [telButton addTarget:self action:@selector(telAction) forControlEvents:UIControlEventTouchUpInside];
        _telNum = poiInfo.phone;
        
    }
    
    UILabel *distanceLabel = (UILabel *)[cell.contentView viewWithTag:103];
    CLLocationCoordinate2D  locations = poiInfo.pt;
    CLLocation* newLocation =[[CLLocation alloc]initWithLatitude:locations.latitude longitude:locations.longitude];
    
    float distance = [newLocation distanceFromLocation:[AppDelegate sharedDelegate].userlocation.location];
    distanceLabel.text = [NSString stringWithFormat:@"%.1fm",distance];
    return cell;
}

-(void)telAction{
    
    NSString *string =[NSString stringWithFormat:@"tel://%@",self.telNum];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.eventsDelegate!= nil  && [self.eventsDelegate respondsToSelector:@selector(basetableView:didSelectRowAtIndexPath:)]){
        [self.eventsDelegate basetableView:self didSelectRowAtIndexPath:indexPath];
    }
    
}

-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    if(text == nil)
        text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

@end
